package com.comdefinition.utils

import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

import java.text.SimpleDateFormat

/**
 * Created by hp4664 on 2017/9/14.
 * 日期工具类
 */
@CompileStatic
@TypeChecked
class DateUtil {

    /**
     * 获取当前时间
     * @return
     */
    static Date getDate() {
        Calendar calendar = Calendar.getInstance()
        return calendar.time
    }

    /**
     * 获取当前的毫秒数
     * @return
     */
    static long getCurrentTimeMillis() {
        Calendar calendar = Calendar.getInstance()
        return calendar.timeInMillis
    }

    /**
     * 字符串 转化为日期
     * @param dateStr 日期字符串
     * @return
     */
    static Date parseDate(String dateStr) {
        return parseDate(dateStr, "yyyy-MM-dd HH:mm:ss")
    }

    static Date parseDate(Long dateTime) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTimeInMillis(dateTime)
        return calendar.time
    }

    /**
     * 将字符串 转化为日期
     * @param dateStr 日期字符串
     * @param pattern 日期格式
     * @return
     */
    static Date parseDate(String dateStr, String pattern) {
        if (!dateStr) return null
        SimpleDateFormat format = new SimpleDateFormat(pattern)
        return format.parse(dateStr)
    }

    /**
     * 日期转化为 字符串
     * @param date
     * @param pattern
     * @return
     */
    static String formateDate(Date date, String pattern) {
        if (!date) return null
        if (!pattern) pattern = "yyyy-MM-dd HH:mm:ss"
        SimpleDateFormat format = new SimpleDateFormat(pattern)
        return format.format(date)
    }

    /**
     * 日期转化为 yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    static String formateDate(Date date) {
        return formateDate(date, "")
    }

    static String formateDate(Long dateTime) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTimeInMillis(dateTime)
        return formateDate(calendar.time, "")
    }

    static String formateDate(Long dateTime, String pattern) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTimeInMillis(dateTime)
        return formateDate(calendar.time, pattern)
    }

    static Long formateDateTime(String dateStr){
        if (!dateStr) return null
        return parseDate(dateStr).time
    }

    /**
     * 获取 00:00:00 对应的毫秒数
     * @return
     */
    static Long getDateFirstTime() {
        Calendar calendar = Calendar.getInstance()
        def str=calendar.format("yyyy-MM-dd 00:00:00")
        return formateDateTime(str)
    }
    static Long getDateFirstTime(Long dateTime) {
        return formateDateTime(getDateFirst(dateTime))
    }
    /**
     * 获取毫秒数 对应日期的00:00:00
     * @param dateTime
     * @return
     */
    static String getDateFirst(Long dateTime) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTimeInMillis(dateTime)
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00")
        return format.format(calendar.time)
    }

    static String getDateFirst(Date date) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTime(date)
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00")
        return format.format(calendar.time)
    }

    /**
     * 获取23:59:59 的毫秒数
     * @return
     */
    static Long getDateLastTime() {
        Calendar calendar = Calendar.getInstance()
        def str= calendar.format("yyyy-MM-dd 23:59:59")
        return formateDateTime(str)
    }
    static Long getDateLastTime(Long dateTime) {
        return formateDateTime(getDateLast(dateTime))
    }
    /**
     *  获取毫秒数 对应日期的23:59:59 字符串
     * @param dateTime
     * @return
     */
    static String getDateLast(Long dateTime) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTimeInMillis(dateTime)
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59")
        return format.format(calendar.time)
    }

    static String getDateLast(Date date) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTime(date)
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59")
        return format.format(calendar.time)
    }

    /**
     * 获取指定天数后的日期
     * @param date
     * @param days 正负
     * @return
     */
    static Date addDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTime(date)
        calendar.add(Calendar.DAY_OF_YEAR, days)
        return calendar.time
    }

    /**
     * 获取月份
     * @param dateTime
     * @return
     */
    static int getMonth(Long dateTime) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTimeInMillis(dateTime)
        return calendar.get(Calendar.MONTH)+1
    }
    static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance()
        calendar.setTime(date)
        return calendar.get(Calendar.MONTH)+1
    }

    /**
     * 2个时间 之间的天数差
     * @param dayTime1
     * @param dayTime2
     * @return
     */
    static int dayDiff(Long dayTime1, Long dayTime2) {
        long daymm1 = parseDate(getDateFirst(dayTime1)).time
        long daymm2 = parseDate(getDateFirst(dayTime2)).time
        return ((daymm1 - daymm2) / (1000 * 60 * 60 * 24)).intValue()
    }

    static int dayDiff(Date day1, Date day2) {
        long daymm1 = parseDate(getDateFirst(day1)).time
        long daymm2 = parseDate(getDateFirst(day2)).time
        return ((daymm1 - daymm2) / (1000 * 60 * 60 * 24)).intValue()
    }


}
